﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Hammock.Web;
using Harvest.Api.ExtensionMethods;
using Harvest.Api.Interfaces;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;

namespace Harvest.Api
{

    /// <summary>
    /// 
    /// </summary>
    public class TimeTrackingsRepository : RepositoryBase
    {

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="provider">The RestRequestProvider for connecting to the service.</param>
        public TimeTrackingsRepository(IRestRequestProvider provider) :
            base(provider) { }


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public Client GetById(int id)
        //{

        //    if (id <= 0) throw new ArgumentOutOfRangeException();

        //    var client = GetRestClient();
        //    var request = Provider.RestRequest;
        //    request.Path = "clients/" + id;
        //    request.Method = WebMethod.Get;

        //    var response = client.Request<ClientItem>(request);

        //    var statusCode = response.StatusCode;
        //    if (statusCode == HttpStatusCode.NotFound) throw new KeyNotFoundException();

        //    var result = response.ContentEntity;

        //    return result.ToClient();
        //}


        /// <summary>
        /// 
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public DailyItem GetForDay(DateTime? day = null)
        {

            if (day.HasValue && day >= DateTime.Now) throw new ArgumentOutOfRangeException();

            var client = GetRestClient();
            var request = Provider.RestRequest;
            request.Path = "daily";
            request.Method = WebMethod.Get;
            if (day.HasValue)
            {
                request.Path += "/" + day.Value.DayOfYear + "/" + day.Value.Year;
            }
            var response = client.Request<DailyItem>(request);

            var statusCode = response.StatusCode;
            if (statusCode == HttpStatusCode.NotFound) throw new KeyNotFoundException();

            return response.ContentEntity;
        }


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public Client Add(Client entity)
        //{

        //    if (entity == null) throw new ArgumentNullException();
        //    if (entity.Name.IsNullOrEmpty()) throw new ArgumentNullException();

        //    var client = GetRestClient();
        //    var request = Provider.RestRequest;
        //    request.Path = "clients";
        //    request.Method = WebMethod.Post;
        //    request.Entity = entity.ToClientItem();

        //    var response = client.Request(request);

        //    var statusCode = response.StatusCode;
        //    if (statusCode == HttpStatusCode.BadRequest) throw new InvalidOperationException();

        //    var locationHeader = response.Headers["Location"];
        //    var id = int.Parse(locationHeader.Substring(locationHeader.LastIndexOf(@"/") + 1));

        //    var result = GetById(id);

        //    return result;
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="id"></param>
        //public void Remove(int id)
        //{

        //    if (id <= 0) throw new ArgumentOutOfRangeException();

        //    var client = GetRestClient();
        //    var request = Provider.RestRequest;
        //    request.Path = "clients/" + id;
        //    request.Method = WebMethod.Delete;
        //    var response = client.Request(request);

        //    var statusCode = response.StatusCode;
        //    if (statusCode == HttpStatusCode.BadRequest) throw new InvalidOperationException();
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="entity"></param>
        //public void Remove(Client entity)
        //{

        //    if (entity == null) throw new ArgumentNullException();
        //    if (!entity.Id.HasValue) throw new ArgumentNullException();

        //    Remove(entity.Id.Value);
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public Client Update(Client entity)
        //{

        //    if (entity == null) throw new ArgumentNullException();
        //    if (!entity.Id.HasValue) throw new ArgumentNullException();
        //    if (entity.Name.IsNullOrEmpty()) throw new ArgumentNullException();

        //    var client = GetRestClient();
        //    var request = Provider.RestRequest;
        //    request.Path = "clients/" + entity.Id.Value;
        //    request.Method = WebMethod.Put;
        //    request.Entity = entity.ToClientItem();

        //    var response = client.Request(request);

        //    var statusCode = response.StatusCode;
        //    if (statusCode == HttpStatusCode.BadRequest) throw new InvalidOperationException();

        //    var locationHeader = response.Headers["Location"];
        //    var id = int.Parse(locationHeader.Substring(locationHeader.LastIndexOf(@"/") + 1));

        //    var result = GetById(id);

        //    return result;

        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="id"></param>
        //public void Deactivate(int id)
        //{
        //    var client = GetRestClient();
        //    var request = Provider.RestRequest;
        //    request.Path = "clients/" + id + "/toggle";
        //    request.Method = WebMethod.Post;
        //    request.Entity = new ClientItem();

        //    var response = client.Request(request);

        //    var statusCode = response.StatusCode;
        //    if (statusCode == HttpStatusCode.BadRequest) throw new InvalidOperationException();
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="entity"></param>
        //public void Deactivate(Client entity)
        //{

        //    if (entity == null) throw new ArgumentNullException();
        //    if (!entity.Id.HasValue) throw new ArgumentNullException();

        //    Deactivate(entity.Id.Value);

        //}

    }

}
